package net.cyl.controller;


import net.cyl.controller.request.ShortLinkAddRequest;
import net.cyl.controller.request.ShortLinkDelRequest;
import net.cyl.controller.request.ShortLinkPageRequest;
import net.cyl.controller.request.ShortLinkUpdateRequest;
import net.cyl.service.ShortLinkService;
import net.cyl.util.JsonData;
import net.cyl.vo.ShortLinkVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 曹玉林
 * @since 2025-05-14
 */
@RestController
@RequestMapping("/api/link/v1")
public class ShortLinkController {
    @Autowired
    private ShortLinkService shortLinkService;

    @PostMapping("/create")
    public JsonData createShortLink(@RequestBody ShortLinkAddRequest request){
        return shortLinkService.createShortLink(request);
    }
    @Value("${rpc.token}")
    private String rpcToken;


    @GetMapping("check")
    public JsonData check(@RequestParam("shortLinkCode") String shortLinkCode, HttpServletRequest request){

        String token = request.getHeader("rpc-token");

        if(rpcToken.equalsIgnoreCase(token)){

            ShortLinkVO shortLinkVO = shortLinkService.parseShortLinkByCode(shortLinkCode);
            return shortLinkVO == null ? JsonData.buildError("短链不存在"):JsonData.buildSuccess();

        }else {
            return JsonData.buildError("非法访问");
        }

    }
    @PostMapping("/page")
    public JsonData pageShortLink(@RequestBody ShortLinkPageRequest request){
        Map<String, Object> result = shortLinkService.pageByGroupId(request);
        return JsonData.buildSuccess(result);

    }


    @PostMapping("/delete")
    public JsonData deleteShortLink(@RequestBody ShortLinkDelRequest request){
        return  shortLinkService.deleteShortLink(request);

    }

    @PostMapping("/update")
    public JsonData updateShortLink(@RequestBody ShortLinkUpdateRequest request){
        return shortLinkService.updateShortLink(request);

    }
}

